library(tidyverse)
## ── Attaching packages ───── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.3 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ──────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
install.packages("tidyverse")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/4.0'
## (as 'lib' is unspecified)
surveys_complete <- read_csv("data/surveys_complete.csv")
## Parsed with column specification:
## cols(
## record_id = col_double(),
## month = col_double(),
## day = col_double(),
## year = col_double(),
## plot_id = col_double(),
## species_id = col_character(),
## sex = col_character(),
## hindfoot_length = col_double(),
## weight = col_double(),
## genus = col_character(),
## species = col_character(),
## taxa = col_character(),
## plot_type = col_character()
## )
Plotting with ggplot2
# Aesthetic mapping
ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length))

# Add a geom - graphical representations
ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
geom_point()

# Assign plot to a variable
surveys_plot <- ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length))
#Draw the plot
surveys_plot + geom_point()

Challenge 1
library(hexbin)
# Hexagonal bin plot - better representation of data with a large num of observations
surveys_plot +
geom_hex()

Building Plots Iteratively
ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
geom_point()

# transparent(alpha) to avoid overplotting
ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
geom_point(alpha = 0.1)

# add colors for all the points
ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length)) +
geom_point(alpha = 0.1, color = "blue")

# color each speices in a plot differently / aes(color = species_id)
ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length)) +
geom_point(alpha = 0.1, aes(color = species_id))

Challenge 2
ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
geom_point(aes(color = plot_type))

Boxplot
# visualize the distribution of weight within each species
ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
geom_boxplot()

# Add points to the boxplot - better idea of num of measurements and distribution
# use geom_jitter()
ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
geom_boxplot(alpha = 0) + #makes bg transparent
geom_jitter(alpha = 0.3, color = "tomato")

Challenge 3
# replace boxplot with violin plot - shape (of the density of point) is drawn
# use geom_violin()
ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
geom_violin()

# Represent weight on the log10 scale - better distribute the obvs
# use scale_y_log10()
ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
geom_violin() +
scale_y_log10()

# Distribution of hindfoot_length within each species
ggplot(data = surveys_complete, mapping = aes(x = species_id, y = hindfoot_length)) +
geom_boxplot(alpha = 0) +
geom_jitter(alpha = 0.3, aes(color = plot_id))

# change plot_id to a factor
plot_id <- factor()
ggplot(data = surveys_complete, mapping = aes(x = species_id, y = hindfoot_length)) +
geom_boxplot(alpha = 0) +
geom_jitter(alpha = 0.3, aes(color = plot_id))

Plotting Time Series Data
yearly_counts <- surveys_complete %>%
count(year,genus)
ggplot(data = yearly_counts, aes(x = year, y = n))+
geom_line()

# draw a line for each genus
ggplot(data = yearly_counts, aes(x= year, y = n, group = genus))+
geom_line()

# add color, automatically groups the data
ggplot(data = yearly_counts, aes(x = year, y = n, color = genus)) +
geom_line()

yearly_counts %>%
ggplot(mapping = aes(x = year, y = n, color = genus))+
geom_line()

#use pipe to link data manipulation to a data visual
yearly_counts_graph <- surveys_complete %>%
count(year,genus) %>%
ggplot(mapping = aes(x= year, y = n, color = genus)) +
geom_line()
yearly_counts_graph

# splits one plot into multiple plots based on a factor in the dataset (ex: species)
ggplot(data = yearly_counts, aes(x=year, y=n))+
geom_line()+
facet_wrap(facets = vars(genus))

#split the line in each plot by sex of each individual measured
yearly_sex_counts <- surveys_complete %>%
count(year, genus, sex)
#split further with color
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, color = sex))+
geom_line()+
facet_wrap(facets=vars(genus))

#facet both by sex and genus
ggplot(data = yearly_sex_counts,
mapping = aes(x=year, y=n, color=sex)) +
geom_line()+
facet_grid(rows = vars(sex), cols = vars(genus))

# organize panels only by rows (or by columns)
## one column, facet by rows
ggplot(data = yearly_sex_counts,
mapping = aes(x=year, y=n, color=sex))+
geom_line()+
facet_grid(rows=vars(genus))

# one row, facet by column
ggplot(data = yearly_sex_counts,
mapping = aes(x = year, y = n, color = sex)) +
geom_line()+
facet_grid(cols=vars(genus))

ggplot2 Themes
ggplot(data = yearly_sex_counts,
mapping = aes(x=year, y=n, color=sex))+
geom_line()+
facet_wrap(vars(genus))+
theme_bw()

Challenge 4
yearly_weight<- surveys_complete %>%
group_by(year,species_id) %>%
summarize(avg_weight= mean(weight))
## `summarise()` regrouping output by 'year' (override with `.groups` argument)
ggplot(data = yearly_weight,
mapping = aes(x=year, y=avg_weight))+
geom_line()+
facet_wrap(vars(species_id))+
theme_bw()

Customization
#add titles & axes labels
ggplot(data = yearly_sex_counts, aes(x=year, y=n, color=sex)) +
geom_line()+
facet_wrap(vars(genus))+
labs(title = "Observed genera through time",
x = "Year of observation",
y = "Number of individuals")+
theme_bw()

#improve readability by increasing font size
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, color = sex)) +
geom_line() +
facet_wrap(vars(genus)) +
labs(title = "Observed genera through time",
x = "Year of observation",
y = "Number of individuals") +
theme_bw() +
theme(text=element_text(size=16))

# customize axis color, size, and angle
# use strip.text to italicize genus name
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, color = sex)) +
geom_line() +
facet_wrap(vars(genus)) +
labs(title = "Observed genera through time",
x = "Year of observation",
y = "Number of individuals") +
theme_bw() +
theme(axis.text.x = element_text(colour = "grey20", size = 12, angle = 90, hjust=0.5, vjust=0.5),
axis.text.y = element_text(colour = "grey20", size = 12),
strip.text = element_text(face ="italic"),
text = element_text(size=16))

# save changes to a default theme to easily apply it to other plots you create
grey_theme <- theme(axis.text.x = element_text(colour="grey20", size = 12,
angle = 90, hjust = 0.5,
vjust = 0.5),
axis.text.y = element_text(colour = "grey20", size = 12),
text=element_text(size = 16))
ggplot(surveys_complete, aes(x = species_id, y = hindfoot_length)) +
geom_boxplot() + grey_theme #created default theme

Challenge 5
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, color = sex)) +
geom_line() +
facet_wrap(vars(genus)) +
labs(title = "Observed genera through time",
x = "Year of observation",
y = "Number of individuals",
color = "Sex of individual") +
scale_color_brewer(palette ="Set1", labels = c("Female","Male"))+
theme_light() +
theme(axis.text.x = element_text(colour = "grey20", size = 12, angle = 90, hjust=0.5, vjust=0.5),
axis.text.y = element_text(colour = "grey20", size = 12),
strip.text = element_text(face ="italic"),
text = element_text(size=16))

Arranging and Exporting Plots
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
spp_weight_boxplot <- ggplot(data = surveys_complete,
aes(x = species_id, y = weight)) +
geom_boxplot() +
labs(x = "Species",
y = expression(log[10](Weight))) +
scale_y_log10() +
labs()
spp_count_plot <- ggplot(data = yearly_counts,
aes(x = year, y = n, color = genus)) +
geom_line() +
labs(x = "Year", y = "Abundance")
grid.arrange(spp_weight_boxplot, spp_count_plot, ncol = 2, widths = c(4, 6))

my_plot <- ggplot(data = yearly_sex_counts,
aes(x = year, y = n, color = sex)) +
geom_line() +
facet_wrap(vars(genus)) +
labs(title = "Observed genera through time",
x = "Year of observation",
y = "Number of individuals") +
theme_bw() +
theme(axis.text.x = element_text(colour = "grey20", size = 12, angle = 90,
hjust = 0.5, vjust = 0.5),
axis.text.y = element_text(colour = "grey20", size = 12),
text = element_text(size = 16))
ggsave("name_of_file.png", my_plot, width = 15, height = 10)
## This also works for grid.arrange() plots
combo_plot <- grid.arrange(spp_weight_boxplot, spp_count_plot, ncol = 2,
widths = c(4, 6))

ggsave("combo_plot_abun_weight.png", combo_plot, width = 10, dpi = 300)
## Saving 10 x 5 in image